package com.cn.wanxi.service;

import com.cn.wanxi.IO.ProductModelIO;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.IOReadWrite;
import com.cn.wanxi.util.Path;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProductService {
    /**
     * 用于展示网站首页的产品信息
     *
     * @return
     */
    public List<ProductModel> getProductModelList() {
        /**
         * 1.写
         * 2.读
         * 3.封装
         */
        writeNote(Path.PATH.getPath(), Path.PRODUCT_NAME.getPath(), Path.SPLIT.getPath());
        String result = new IOReadWrite().readNote(Path.PATH.getPath(), Path.PRODUCT_NAME.getPath());
        return setData(result);
    }

    public static void main(String[] args) {
        new ProductService().getProductModelList();
    }

    private List<ProductModel> setData(String result) {
        String[] results = result.split(Path.SPLIT.getPath());
        String imgHref = results[0];
        String title = results[1];
        String content = results[2];
        String detailHref = results[3];
        imgHref = imgHref.replace("[", "").replace("]", "");
        title = title.replace("[", "").replace("]", "");
        content = content.replace("[", "").replace("]", "");
        detailHref = detailHref.replace("[", "").replace("]", "");

        List<ProductModel> list = new ArrayList<>();
        ProductModel productModel = new ProductModel();
        String[] imgHrefs = imgHref.split(",");
        String[] titles = title.split(",");
        String[] contents = content.split(",");
        String[] detailHrefs = detailHref.split(",");
        for (int i = 0; i < imgHrefs.length; i++) {
            ProductModel model = new ProductModel();
            model.setImgHref(imgHrefs[i]);
            model.setTitle(titles[i]);
            model.setContent(contents[i].replace("%%%",","));
            model.setDetailHref(detailHrefs[i]);
            list.add(model);
        }
        return list;
    }

    private void writeNote(String path, String fileName, String split) {
        File directory = new File(path);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        File file = new File(directory, fileName);
        try {
            file.createNewFile();
            OutputStream outputStream = new FileOutputStream(file);
            ProductModelIO productModelIO = new ProductModelIO();

            outputStream.write(Arrays.toString(productModelIO.getImgHrefs()).getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(Arrays.toString(productModelIO.getTitles()).getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(Arrays.toString(productModelIO.getContents()).getBytes());
            outputStream.write(split.getBytes());
            outputStream.write(Arrays.toString(productModelIO.getDetailHrefs()).getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据用于展示公司介绍页面的图片轮播
     *
     * @return
     */
    public List<ProductModel> getProductModelListForCompanyPage() {
        List<ProductModel> list = new ArrayList<>();
        ProductModel productModel = new ProductModel();
        String[] imgHrefs = productModel.getImgHrefForCompanyPage();
        for (int i = 0; i < imgHrefs.length; i++) {
            ProductModel model = new ProductModel();
            model.setImgHref(imgHrefs[i]);
            list.add(model);
        }
        return list;
    }
}
